﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using iHub.App_Code;
using iHub.Properties;
using System.Collections;
using System.Text.RegularExpressions;

namespace iHub
{
    public partial class main : Form
    {
        Image img;
        Image back;

        //For Employees
        Boolean currentStatus = true;
        AssignmentManager assm = new AssignmentManager();
        EmployeeManager en = new EmployeeManager();
        AccountManager acctm = new AccountManager();
        PayrollManager paym = new PayrollManager();
        StringValidator validator = new StringValidator();
        ExcelHandler ex = new ExcelHandler();

        //For Payroll
        DataTable dt = new DataTable();

        DataTable sana = new DataTable();
        DataSet sana1 = new DataSet();

        public static main form = null;
        String userType = "";

        //For Employees

        //For Payroll
        public static Account nacct = new Account();
        private static String user;
        String pass;
        String date;
        String time;
        private static Boolean usertype = false;
        String empname;
        private static int biryear = 0;
        int advances;
        DataTable cv = new DataTable();
        DataTable plist = new DataTable();
        DataTable bir = new DataTable();
        DateTime cvdate = new DateTime();
        private static DateTime listbegin = new DateTime();
        private static DateTime listend = new DateTime();
        private static bool birdocutype = false;

        public main(Account a, String d, String t)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Start_Closing);

            try
            {
                back = Resources.menuButton;
                img = Resources.menuClick;

                nacct = a;

                user = nacct.getUsername();
                pass = nacct.getPass();
                usertype = nacct.getUsertype();

                date = d;
                time = t;
                lUser.Text = "Welcome, " + user + ".";

                Reminders.Text = "Reminders:\n";
                clockTimer.Start();

                if (DateTime.Today.DayOfWeek == DayOfWeek.Thursday)
                {
                    remPayroll.ForeColor = Color.Red;
                    remPayroll.Text = "- Print Payroll List\n";
                }
                else
                {
                    remPayroll.Text = "";
                }

                if (DateTime.Today.Day == 1)
                {
                    remBIR.ForeColor = Color.Red;
                    remBIR.Text = "- Print BIR Report\n";
                }
                else
                {
                    remBIR.Text = "";
                }

                if (usertype)
                {
                    userType = "Administrator Rights";
                }
                else
                {
                    userType = "No Administrator Rights";
                    acctViewAccts.Enabled = false;
                }

                acctUsername.Text = user;
                acctPassword.Text = "**************";
                acctUserType.Text = userType;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        public String getloggeduser()
        {
            return user;
        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                Application.Exit();
            }
        }

        private void logout_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to Log Out?", "Log Out", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msg == DialogResult.Yes)
            {
                this.Dispose();

                login view = new login();
                view.ShowDialog();
            }
        }

        private void clockTimer_Tick(object sender, EventArgs e)
        {
            lDayOfWeek.Text = "Today is " + DateTime.Today.ToLongDateString() + ", " + DateTime.Now.ToLongTimeString();
        }

        /* -- Payroll Module -- */

        private void payGen_Click(object sender, EventArgs e)
        {
            try
            {
                this.MaximizeBox = true;
                panelPayUpdate.Visible = false;
                payBoxGenEmpName.Items.Clear();
                List<string> emplist = new List<string>();
                panelPayView.Visible = false;
                panelGenerate.Visible = true;
                payGenCV.Enabled = false;
                viewGeneratedCV.Visible = false;

                emplist = en.ViewEmployee();

                for (int i = 0; i < emplist.Count; i++)
                {
                    payBoxGenEmpName.Items.Add(emplist[i]);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void payView_Click(object sender, EventArgs e)
        {
            this.MaximizeBox = true;
            panelPayUpdate.Visible = false;
            panelGenerate.Visible = false;
            panelPayView.Visible = true;
            paySearchBar.Enabled = false;
            payPrintSearch.Enabled = false;
            payPrintDataCV.Visible = false;
            payPrintDataPList.Visible = false;
            payPrintListBIR.Visible = false;
        }

        private void payUpdate_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.MaximizeBox = false;
            panelGenerate.Visible = false;
            panelPayView.Visible = false;
            panelPayUpdate.Visible = true;
            panelPayAdd.Visible = false;
            panelPayModify.Visible = false;
        }

        private void payGenCV_Click(object sender, EventArgs e)
        {
            DialogResult rm = MessageBox.Show("Add cash advances?", "Cash Advances", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            try
            {
                if (rm == DialogResult.Yes)
                {
                    form = this;
                    cashadvances a = new cashadvances();
                    a.Show();
                    this.Enabled = false;
                }
                else if (rm == DialogResult.No)
                {
                    advances = 0;
                    paycreateCV();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        public void paysetAdvances(int a)
        {
            try
            {
                advances = a;
                paycreateCV();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void paycreateCV()
        {
            ArrayList assigns = new ArrayList();
            String s;
            string[] remove;
            String no;
            int x;
            int y = Convert.ToInt32(empname);
            bool check = false;
            DataTable cv = new DataTable();
            DateTime date = DateTime.Now;

            try
            {
                check = paym.checkPay(y);

                if (payListGenerateAss.Items.Count > 0)
                {
                    if (check)
                    {
                        for (int i = 0; i < payListGenerateAss.Items.Count; i++)
                        {
                            s = payListGenerateAss.Items[i].ToString();
                            remove = s.Split('-');
                            no = remove[0];
                            x = Convert.ToInt32(no);
                            assigns.Add(x);
                        }

                        paym.CreateVoucher(assigns, y, advances);
                        cv = paym.viewVoucher(y, date);
                        viewGeneratedCV.DataSource = cv;
                        viewGeneratedCV.Visible = true;
                    }
                    else
                    {
                        MessageBox.Show("Unable to create cash voucher because last payment was created less than 7 days ago", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    MessageBox.Show("No available assignments", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void payGenSearch_Click(object sender, EventArgs e)
        {
            payListGenerateAss.Items.Clear();
            List<string> accassign = new List<string>();

            try
            {
                if (payBoxGenEmpName.SelectedIndex != -1)
                {
                    accassign = en.ViewAccAssignments(empname);
                    for (int i = 0; i < accassign.Count; i++)
                    {
                        payListGenerateAss.Items.Add(accassign[i]);
                    }
                    payGenCV.Enabled = true;
                }
                else
                {
                    MessageBox.Show("No employee selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void payPrintCVouch_Click(object sender, EventArgs e)
        {
            voucherdate f = new voucherdate();
            f.Show();
            form = this;
            this.Enabled = false;
            paySearchBar.Enabled = true;
            payPrintSearch.Enabled = true;
            payPrintDataCV.Visible = true;
            payPrintDataPList.Visible = false;
            payPrintListBIR.Visible = false;
        }

        public void setCVDate(DateTime d)
        {
            cvdate = d;
        }

        private void payPrintPList_Click(object sender, EventArgs e)
        {
            payrolldate f = new payrolldate();
            f.Show();
            form = this;
            this.Enabled = false;
            paySearchBar.Text = "";
            paySearchBar.Enabled = false;
            payPrintSearch.Enabled = false;
            payPrintDataCV.Visible = false;
            payPrintDataPList.Visible = true;
            payPrintListBIR.Visible = false;
        }

        public void getPList(DateTime a, DateTime b)
        {
            payDataDisplay.DataSource = null;
            listbegin = a;
            listend = b;
            try
            {
                plist = paym.viewList(listbegin, listend);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(),"Fatal",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }

            payDataDisplay.DataSource = plist;
        }

        private void payPrintBIR_Click(object sender, EventArgs e)
        {
            birmonth f = new birmonth();
            f.Show();
            form = this;
            this.Enabled = false;
            paySearchBar.Text = "";
            paySearchBar.Enabled = false;
            payPrintSearch.Enabled = false;
            payPrintDataCV.Visible = false;
            payPrintDataPList.Visible = false;
            payPrintListBIR.Visible = true;
        }

        public void getBIR(int m,int year,bool type)
        {
            payDataDisplay.DataSource = null;
            biryear = year;
            birdocutype = type;
            try
            {
                bir = paym.viewBIR(m, year);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(),"Fatal",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }

            payDataDisplay.DataSource = bir;
        }

        public void getBIR(int year, bool type)
        {
            payDataDisplay.DataSource = null;
            biryear = year;
            birdocutype = type;
            try
            {
                bir = paym.viewBIR(year);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(),"Fatal",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }

            payDataDisplay.DataSource = bir;
        }

        private void payBoxGenEmpName_SelectedIndexChanged(object sender, EventArgs e)
        {
            String item = payBoxGenEmpName.SelectedItem.ToString();
            String name2, id2;
            string[] words1 = item.Split('-');

            name2 = words1[0];
            id2 = words1[1];

            empname = id2;
        }

        private void payPrintSearch_Click(object sender, EventArgs e)
        {
            try
            {
                payDataDisplay.DataSource = null;
                int empid = Convert.ToInt32(paySearchBar.Text);
                cv = paym.viewVoucher(empid, cvdate);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }

            payDataDisplay.DataSource = cv;
        }

        /* -- Employee Module -- */

        private void empView_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.MaximizeBox = false;
            ViewBox.Visible = true;
            AddBox.Visible = false;
            EditBox.Visible = false;
            empViewList.DataSource = en.ViewEmployee();

            empViewID.Text = null;
            empViewTIN1.Text = null;
            empViewTIN2.Text = null;
            empViewTIN3.Text = null;
            empViewAddress.Text = null;
            empViewCity.Text = null;
            empViewContact.Text = null;

            empViewList.SelectedIndex = -1;
        }

        private void empAdd_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.MaximizeBox = false;
            ViewBox.Visible = false;
            AddBox.Visible = true;
            EditBox.Visible = false;

            empAddTIN1.Text = null;
            empAddTIN2.Text = null;
            empAddTIN3.Text = null;
            empAddName.Text = null;
            empAddAddress.Text = null;
            empAddCity.Text = null;
            empAddContact.Text = null;
        }

        private void empEdit_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.MaximizeBox = false;
            ViewBox.Visible = false;
            AddBox.Visible = false;
            EditBox.Visible = true;
            empEditID.Text = null;
            empEditTIN1.Text = null;
            empEditTIN2.Text = null;
            empEditTIN3.Text = null;
            empEditAddress.Text = null;
            empEditCity.Text = null;
            empEditContact.Text = null;

            empEditList.DataSource = assm.EmployeeAssign();
            empEditID.Text = null;
            empEditTIN1.Text = null;
            empEditTIN2.Text = null;
            empEditTIN3.Text = null;
            empEditAddress.Text = null;
            empEditCity.Text = null;
            empEditContact.Text = null;
            empEditList.SelectedIndex = -1;
        }

        private void empVChangeStatus_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentStatus == true)
                {
                    empVChangeStatus.Text = "Terminate";

                    DialogResult msg = MessageBox.Show("Are you sure you terminate this employee?", "Terminate Employee", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                    if (msg == DialogResult.Yes)
                    {
                        msg = MessageBox.Show("Employee Terminated.", "Terminate Employee", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        currentStatus = false;

                        empVChangeStatus.Text = "Hire Back";
                    }
                }
                else if (currentStatus == false)
                {
                    empVChangeStatus.Text = "Hire Back";

                    DialogResult msg = MessageBox.Show("Are you sure you hire this employee back?", "Hire Back Employee", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                    if (msg == DialogResult.Yes)
                    {
                        msg = MessageBox.Show("Employee Hired Back.", "Hire Back Employee", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        currentStatus = true;

                        empVChangeStatus.Text = "Terminate";
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        /* -- Account Module -- */

        private void acctEdit_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.MaximizeBox = false;
            panelEditAccount.Visible = true;
            panelLoginHistory.Visible = false;
            panelViewAccts.Visible = false;
        }

        private void acctEditPassword_Click(object sender, EventArgs e)
        {
            panelTerminate.Visible = false;
            panelEditSecurity.Visible = false;
            panelEditPassword.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panelTerminate.Visible = true;
            panelEditPassword.Visible = false;
            panelEditSecurity.Visible = false;
        }

        private void acctViewHistory_Click(object sender, EventArgs e)
        {
            this.MaximizeBox = true;
            panelEditAccount.Visible = false;
            panelLoginHistory.Visible = true;
            panelViewAccts.Visible = false;

            //--------------------------------

            acctLoginHistory.DataSource = acctm.viewLoginHistory();
        }

        private void acctViewAccts_Click(object sender, EventArgs e)
        {
            this.MaximizeBox = true;
            panelEditAccount.Visible = false;
            panelLoginHistory.Visible = false;
            panelViewAccts.Visible = true;

            //--------------------------------

            try
            {
                acctViewUser.DataSource = acctm.viewUserAccounts();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        public void acctRefresh()
        {
            try
            {
                acctViewUser.DataSource = acctm.viewUserAccounts();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void acctEditSecurity_Click(object sender, EventArgs e)
        {
            panelTerminate.Visible = false;
            panelEditPassword.Visible = false;
            panelEditSecurity.Visible = true;
        }

        private void acctChangePassword_Click(object sender, EventArgs e)
        {
            String newpass = "";
            try
            {
                if (validator.IsSafeString(acctCurPassword.Text) && validator.IsSafeString(acctVerifyPassword.Text) && validator.IsSafeString(acctNewPassword.Text))
                {
                    if (pass == acctCurPassword.Text && acctVerifyPassword.Text == acctNewPassword.Text && acctNewPassword.Text != pass && acctNewPassword.Text != "" && acctVerifyPassword.Text != "")
                    {
                        DialogResult msg = MessageBox.Show("Are you sure you want to change your password?", "Update Password", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                        if (msg == DialogResult.Yes)
                        {
                            newpass = acctNewPassword.Text;
                            acctm.editPassword(user, newpass);

                            acctCurPassword.Text = "";
                            acctVerifyPassword.Text = "";
                            acctNewPassword.Text = "";

                            MessageBox.Show("Password has been successfully updated, you will be automatically logged out for the changes to take effect", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Dispose();

                            login view = new login();
                            view.ShowDialog();
                        }
                        else
                        {
                            acctCurPassword.Text = "";
                            acctVerifyPassword.Text = "";
                            acctNewPassword.Text = "";
                        }
                    }
                    else if (acctCurPassword.Text == "" || acctNewPassword.Text == "" || acctVerifyPassword.Text == "")
                    {
                        MessageBox.Show("Please make sure that all fields are properly encoded", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                        acctCurPassword.Text = "";
                        acctVerifyPassword.Text = "";
                        acctNewPassword.Text = "";
                    }
                    else if (pass != acctCurPassword.Text)
                    {
                        MessageBox.Show("Password is does not match with this account", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                        acctCurPassword.Text = "";
                        acctVerifyPassword.Text = "";
                        acctNewPassword.Text = "";
                    }
                    else if (acctNewPassword.Text != acctVerifyPassword.Text)
                    {
                        MessageBox.Show("Please confirm that your new password is correct", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                        acctCurPassword.Text = "";
                        acctVerifyPassword.Text = "";
                        acctNewPassword.Text = "";
                    }
                    else if (acctNewPassword.Text == pass)
                    {
                        MessageBox.Show("Please enter a new password different from your current password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                        acctCurPassword.Text = "";
                        acctVerifyPassword.Text = "";
                        acctNewPassword.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        private void acctChangeSecurity_Click(object sender, EventArgs e)
        {
            String newquest = "";
            String newans = "";
            try
            {
                if (validator.IsSafeString(acctSecPass.Text) && validator.IsSafeString(acctSecAns.Text) && validator.IsSafeString(acctSecQuest.Text))
                {
                    if (pass == acctSecPass.Text && acctSecAns.Text != "" && acctSecQuest.Text != "")
                    {
                        DialogResult msg = MessageBox.Show("Are you sure you want to change your security parameters?", "Update Security Details", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                        if (msg == DialogResult.Yes)
                        {
                            newquest = acctSecQuest.Text;
                            newans = acctSecAns.Text;
                            acctm.editSecurity(user, newquest, newans);
                            MessageBox.Show("Security Question and Answer have been successfully updated", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            acctSecPass.Text = "";
                            acctSecQuest.Text = "";
                            acctSecAns.Text = "";
                        }
                        else
                        {
                            acctSecPass.Text = "";
                            acctSecQuest.Text = "";
                            acctSecAns.Text = "";
                        }
                    }
                    else if (acctSecPass.Text == "" || acctSecQuest.Text == "" || acctSecAns.Text == "")
                    {
                        MessageBox.Show("Please make sure that all fields are properly encoded", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                        acctSecPass.Text = "";
                        acctSecQuest.Text = "";
                        acctSecAns.Text = "";
                    }
                    else if (pass != acctSecPass.Text)
                    {
                        MessageBox.Show("Password is does not match with this account", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                        acctSecPass.Text = "";
                        acctSecQuest.Text = "";
                        acctSecAns.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        private void acctTerminateConf_Click(object sender, EventArgs e)
        {
            bool check = false;
            try
            {
                if (pass == acctTermPassword.Text)
                {
                    DialogResult msg = MessageBox.Show("Are you sure you want to terminate your account?", "Terminate Account", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                    if (msg == DialogResult.Yes)
                    {
                        if (usertype)
                        {
                            try
                            {
                                check = acctm.findAccount();

                                if (check)
                                {
                                    msg = MessageBox.Show("Account Terminated. You will now be logged off the system.", "Account Terminated", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                                    acctm.terminateAccount(user);
                                    this.Dispose();

                                    login view = new login();
                                    view.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("Unable to terminate account because this account is the only administrator present in the system", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                    acctTermPassword.Text = "";
                                }
                            }
                            catch (Exception err)
                            {
                                MessageBox.Show(err.Message+"\nError Stack: "+err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            }
                        }
                        else
                        {
                            try
                            {
                                msg = MessageBox.Show("Account Terminated. You will now be logged off the system.", "Account Terminated", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                                acctm.terminateAccount(user);
                                this.Dispose();

                                login view = new login();
                                view.ShowDialog();
                            }
                            catch (Exception err)
                            {
                                MessageBox.Show(err.Message + "\nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            }
                        }
                    }
                    else
                    {
                        acctTermPassword.Text = "";
                    }
                }
                else if (acctTermPassword.Text == "")
                {
                    MessageBox.Show("Please enter a valid password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    acctTermPassword.Text = "";
                }
                else if (pass != acctTermPassword.Text)
                {
                    MessageBox.Show("Password is does not match with this account", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    acctTermPassword.Text = "";
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        private void acctCreateAcct_Click(object sender, EventArgs e)
        {
            form = this;

            createAccount create = new createAccount();
            create.Show();

            this.Enabled = false;
        }

        private void acctEditAccts_Click(object sender, EventArgs e)
        {
            form = this;

            editAccount edit = new editAccount();
            edit.Show();

            this.Enabled = false;
        }

        /* -- Assignment Module -- */

        private void Print_Click(object sender, EventArgs e)
        {
            form = this;

            printselect print = new printselect();
            print.Show();
            this.Enabled = false;
        }

        private void Print2_Click(object sender, EventArgs e)
        {
            form = this;

            printselect print = new printselect();
            print.Show();
            this.Enabled = false;
        }

        private void assView_Click(object sender, EventArgs e)
        {
            this.MaximizeBox = true;
            this.WindowState = FormWindowState.Maximized;
            panelAssView.Visible = true;
            panelAssEdit.Visible = false;
            panelAssAdd.Visible = false;

            assViewFilter.Items.Clear();
            assViewFilter.Text = "Select Filter";
            assViewFilter.Items.Add("Assign ID");
            assViewFilter.Items.Add("Employee ID");
            assViewFilter.Items.Add("Employee Name");
            assViewFilter.Items.Add("Requesting Unit");
            assViewFilter.Items.Add("Description");
            assViewFilter.Items.Add("Batch No");
            assViewFilter.Items.Add("Status");
            assViewFilter.Items.Add("Start Date");
            assViewFilter.Items.Add("End Date");
            assViewFilter.Items.Add("Address");
            assViewFilter.Items.Add("City");
            assViewFilter.Items.Add("Account Name");
            assViewFilter.Items.Add("Subject Name");
            assViewFilter.Items.Add("Subject Type");
            assViewFilter.Items.Add("C.I.");
            assVFilter.Text = "";
            //--------------------------

            try
            {
                assViewDisplay.DataSource = assm.ViewAssign();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void assAdd_Click(object sender, EventArgs e)
        {
            List<string> a = new List<string>();
            a = assm.getRates();
            panelAssView.Visible = false;
            panelAssEdit.Visible = false;
            panelAssAdd.Visible = true;

            assAccounts.Items.Clear();
            assRequestor.Text = null;
            assSubjectType.SelectedIndex = -1;
            assCI.Text = null;
            assAddRequestingUnit.Text = null;
            assAddBatchNo.Text = null;
            startDate.Text = null;
            endDate.Text = null;
            assAddAddress.Text = null;
            assAddCity.Text = null;
            assAddDesc.Text = null;
            Type1.Text = "Select Type";
            Type1.Items.Clear();
            Type1.Text = "Select Type";
            this.WindowState = FormWindowState.Normal;
            this.MaximizeBox = false;

            try
            {
                for (int i = 0; i < a.Count; i++)
                {
                    Type1.Items.Add(a[i]);
                }

                AssignBox.DataSource = assm.EmployeeAssign();
                AssignBox.SelectedIndex = -1;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        private void assEdit_Click(object sender, EventArgs e)
        {
            this.MaximizeBox = true;
            this.WindowState = FormWindowState.Maximized;
            panelAssView.Visible = false;
            panelAssEdit.Visible = true;
            panelAssAdd.Visible = false;

            assEditFilter.Items.Clear();
            assEditFilter.Text = "Select Filter";
            assEditFilter.Items.Add("Assign ID");
            assEditFilter.Items.Add("Employee ID");
            assEditFilter.Items.Add("Employee Name");
            assEditFilter.Items.Add("Requesting Unit");
            assEditFilter.Items.Add("Description");
            assEditFilter.Items.Add("Batch No");
            assEditFilter.Items.Add("Status");
            assEditFilter.Items.Add("Start Date");
            assEditFilter.Items.Add("End Date");
            assEditFilter.Items.Add("Address");
            assEditFilter.Items.Add("City");
            assEditFilter.Items.Add("Account Name");
            assEditFilter.Items.Add("Subject Name");
            assEditFilter.Items.Add("Subject Type");
            assEditFilter.Items.Add("C.I.");
            assEFilter.Text = "";

            try
            {
                assEditDisplay.DataSource = assm.ViewTable();
                sana = assm.ViewTable();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        public void assEditDisplay_Refresh()
        {
            try
            {
                assEditDisplay.DataSource = assm.ViewTable();
                sana = assm.ViewTable();
                assEFilter.Text = "";
                assVFilter.Text = "";
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "\nError Stack" + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void assEditConf_Click(object sender, EventArgs e)
        {
            string tempid = "";
            List<string> names = new List<string>();

            try{
                DataRow dataRow = sana.Rows[assEditDisplay.CurrentRow.Index];

                tempid = Convert.ToString(dataRow["Assignment ID"]);

                form = this;

                this.Enabled = false;

                Form2 edit = new Form2(tempid);
                edit.Show();
            }
            catch (NullReferenceException error)
            {
                MessageBox.Show("No row selected. \nError Encountered "+error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void assEditCancel_Click_1(object sender, EventArgs e)
        {
            int id = 0;
            DialogResult msg = MessageBox.Show("Are you sure you want to delete this assignment?", "Delete Assignment", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            bool deleted = false;

            try{
                if (msg == DialogResult.Yes)
                {
                    DataRow dataRow = sana.Rows[assEditDisplay.CurrentRow.Index];

                    id = Convert.ToInt32(dataRow["Assignment ID"]);

                    try
                    {
                        deleted = assm.DeleteAssignment(id);

                        if (deleted)
                        {
                            MessageBox.Show("Assignment Deleted", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Unable to delete the Assignment, it has already been completed", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }

                        assEditDisplay_Refresh();
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show("Failed to delete the assignment \n"+err.ToString(),"Fatal",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
            }
            catch (NullReferenceException error)
            {
                MessageBox.Show("No row selected. \nError Encountered " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void empAddConf_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (empAddTIN1.Text == "" || empAddTIN2.Text == "" || empAddTIN3.Text == "" || empAddName.Text == "" || empAddAddress.Text == "" || empAddContact.Text == "" || empAddCity.Text == "")
                {
                    DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (validator.IsSafeString(empAddName.Text) && validator.IsSafeString(empAddAddress.Text) && validator.IsSafeString(empAddCity.Text))
                    {
                        if (empAddTIN1.TextLength == 3 && empAddTIN2.TextLength == 3 && empAddTIN3.TextLength == 3)
                        {
                            Employee thisEmployee = new Employee();

                            String dash = "-";

                            String TIN1 = empAddTIN1.Text, TIN2 = empAddTIN2.Text, TIN3 = empAddTIN3.Text;
                            thisEmployee.TIN = TIN1 += dash += TIN2 += dash += TIN3;
                            thisEmployee.Name = empAddName.Text;
                            thisEmployee.Address = empAddAddress.Text;
                            thisEmployee.City = empAddCity.Text;
                            thisEmployee.ContactNo = empAddContact.Text;
                            thisEmployee.Status = "1";

                            en.InsertEmployee(thisEmployee);
                            empAddTIN1.Text = null;
                            empAddTIN2.Text = null;
                            empAddTIN3.Text = null;
                            empAddName.Text = null;
                            empAddAddress.Text = null;
                            empAddCity.Text = null;
                            empAddContact.Text = null;
                            DialogResult msg1 = MessageBox.Show("The Employee has been added to the Database", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            DialogResult msg = MessageBox.Show("Incorrect TIN", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(),"Fatal",MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void empEditList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (empEditList.SelectedIndex != -1)
                {
                    String item = empEditList.Text;
                    String name2, id2;
                    string[] words1 = item.Split('-');

                    name2 = words1[0];
                    id2 = words1[1];

                    string tin1;
                    string add1;
                    string con1;
                    string city1;

                    empEditID.Text = words1[0];
                    tin1 = en.EmployeeTIN(id2);
                    string[] words = tin1.Split('-');
                    empEditTIN1.Text = words[0];
                    empEditTIN2.Text = words[1];
                    empEditTIN3.Text = words[2];

                    add1 = en.EmployeeAddress(id2);
                    empEditAddress.Text = add1;
                    city1 = en.EmployeeCity(id2);
                    empEditCity.Text = city1;
                    con1 = en.EmployeeContact(id2);
                    empEditContact.Text = con1;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void empEditConf_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (empEditTIN1.Text == "" || empEditTIN2.Text == "" || empEditTIN3.Text == "" || empEditList.Text == "" || empEditAddress.Text == "" || empEditCity.Text == "" || empEditContact.Text == "")
                {
                    DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (validator.IsSafeString(empEditAddress.Text) && validator.IsSafeString(empEditCity.Text))
                    {
                        if (empEditTIN1.TextLength == 3 && empEditTIN2.TextLength == 3 && empEditTIN3.TextLength == 3)
                        {
                            Employee thisEmployee = new Employee();

                            String dash = "-";

                            String item = empEditList.Text;
                            String name2, id2;
                            string[] words1 = item.Split('-');

                            name2 = words1[0];
                            id2 = words1[1];

                            thisEmployee.IdNo = id2;
                            thisEmployee.TIN = empEditTIN1.Text += dash += empEditTIN2.Text += dash += empEditTIN3.Text;
                            thisEmployee.Name = empEditID.Text;
                            thisEmployee.Address = empEditAddress.Text;
                            thisEmployee.City = empEditCity.Text;
                            thisEmployee.ContactNo = empEditContact.Text;

                            en.EditEmployee(thisEmployee);

                            empEditList.SelectedIndex = -1;
                            empEditList.DataSource = assm.EmployeeAssign();
                            empEditID.Text = null;
                            empEditTIN1.Text = null;
                            empEditTIN2.Text = null;
                            empEditTIN3.Text = null;
                            empEditAddress.Text = null;
                            empEditCity.Text = null;
                            empEditContact.Text = null;

                            empEditList.SelectedIndex = -1;
                            DialogResult msg1 = MessageBox.Show("The Employee Information has been updated in the Database", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            DialogResult msg = MessageBox.Show("Incorrect TIN", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void empViewList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (empViewList.SelectedIndex != -1)
            {
                String item = empViewList.Text;
                String name2, id2;
                string[] words1 = item.Split('-');

                name2 = words1[0];
                id2 = words1[1];

                string tin1;
                string add1;
                string con1;
                string city1;

                empViewID.Text = id2;
                tin1 = en.EmployeeTIN(id2);
                string[] words = tin1.Split('-');
                empViewTIN1.Text = words[0];
                empViewTIN2.Text = words[1];
                empViewTIN3.Text = words[2];

                add1 = en.EmployeeAddress(id2);
                empViewAddress.Text = add1;
                city1 = en.EmployeeCity(id2);
                empViewCity.Text = city1;
                con1 = en.EmployeeContact(id2);
                empViewContact.Text = con1;
            }
        }

        private void empViewList_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (empViewList.SelectedIndex != -1)
            {
                String item = empViewList.Text;
                String name2, id2;
                string[] words1 = item.Split('-');

                name2 = words1[0];
                id2 = words1[1];

                string tin1;
                string add1;
                string con1;
                string city1;

                empViewID.Text = name2;
                tin1 = en.EmployeeTIN(id2);
                string[] words = tin1.Split('-');
                empViewTIN1.Text = words[0];
                empViewTIN2.Text = words[1];
                empViewTIN3.Text = words[2];

                add1 = en.EmployeeAddress(id2);
                empViewAddress.Text = add1;
                city1 = en.EmployeeCity(id2);
                empViewCity.Text = city1;
                con1 = en.EmployeeContact(id2);
                empViewContact.Text = con1;

                empAssList.Visible = false;
            }
        }

        private void empVChangeStatus_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (empViewList.Text == "")
                {
                    DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Employee thisEmployee = new Employee();
                    Assignment thisAssignment = new Assignment();

                    String item = empViewList.Text;
                    String name2, id2;
                    string[] words1 = item.Split('-');

                    name2 = words1[0];
                    id2 = words1[1];

                    if (en.EmployeeBoolean(id2))
                    {
                        String dash1 = "-";
                        String Terminate = "*Terminated";

                        thisEmployee.IdNo = id2;
                        thisAssignment.Eid = id2;
                        thisEmployee.Name = name2 += Terminate;
                        thisAssignment.Ename = name2;
                        thisEmployee.Status = "0";

                        en.TerminateEmployee(thisEmployee);
                        assm.UpdateAssign(thisAssignment);
                        DialogResult msg1 = MessageBox.Show("The Employee has been Terminated", "Terminate", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        empViewID.Text = words1[0] += Terminate += dash1 += words1[1];

                        empViewList.DataSource = en.ViewEmployee();
                    }
                    else
                    {
                        DialogResult msg1 = MessageBox.Show("Already Terminated", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void Hire_Click(object sender, EventArgs e)
        {
            try
            {
                if (empViewList.Text == "")
                {
                    DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    String item = empViewList.Text;
                    String name2, id2;
                    string[] words1 = item.Split('-');

                    name2 = words1[0];
                    id2 = words1[1];

                    if (!en.EmployeeBoolean(id2))
                    {
                        Employee thisEmployee = new Employee();
                        Assignment thisAssignment = new Assignment();
                        DialogResult msg1 = MessageBox.Show("The Employee has been Hired Back", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        string name1;

                        name1 = name2;

                        string[] words = name1.Split('*');

                        thisEmployee.Name = words[0];
                        thisAssignment.Ename = words[0];
                        thisEmployee.IdNo = id2;
                        thisAssignment.Eid = id2;
                        thisEmployee.Status = "1";

                        en.HireEmployee(thisEmployee);
                        assm.UpdateAssign(thisAssignment);

                        String dash = "-";
                        empViewID.Text = words[0] += dash += id2;
                        empViewList.DataSource = en.ViewEmployee();
                    }
                    else
                    {
                        DialogResult msg1 = MessageBox.Show("Already Hired", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void assAddConf_Click(object sender, EventArgs e)
        {
            try
            {
                if (assAddRequestingUnit.Text == "" || assAddAddress.Text == "" || assAddCity.Text == "" || assAddBatchNo.Text == "" || startDate.Text == "" || endDate.Text == "" || Type1.Text == "Select Type" || assCI.Text == "" || assSubjectType.Text == "" || assAccounts.Items.Count <= 0)
                {
                    DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (validator.IsSafeString(assAddRequestingUnit.Text) && validator.IsSafeString(assAddAddress.Text) && validator.IsSafeString(assAddCity.Text) && validator.IsSafeString(assAddDesc.Text) && validator.IsSafeString(assCI.Text) && validator.IsSafeString(assRequestor.Text))
                    {
                        if (Convert.ToDateTime(startDate.Text) > Convert.ToDateTime(endDate.Text))
                        {
                            DialogResult msg = MessageBox.Show("Incorrect Date", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            Assignment thisAssignment = new Assignment();

                            String type = Type1.SelectedItem.ToString();
                            String i = "";
                            string[] word2 = type.Split('-');

                            if (AssignBox.SelectedIndex != -1)
                            {
                                String item = AssignBox.Text;
                                String name2, id2;
                                string[] words1 = item.Split('-');

                                name2 = words1[0];
                                id2 = words1[1];

                                thisAssignment.Eid = id2;
                                thisAssignment.Ename = name2;
                            }
                            else
                            {
                                thisAssignment.Eid = "";
                                thisAssignment.Ename = "";
                            }

                            List<string> name = new List<string>();

                            foreach (string nameitem in assAccounts.Items)
                            {
                                name.Add(nameitem);
                            }

                            i = word2[0];

                            thisAssignment.Request = assAddRequestingUnit.Text;
                            thisAssignment.Batch = assAddBatchNo.Text;
                            thisAssignment.Startdate = startDate.Text;
                            thisAssignment.Enddate = endDate.Text;
                            thisAssignment.Address = assAddAddress.Text;
                            thisAssignment.City = assAddCity.Text;
                            thisAssignment.Assign = assAddDesc.Text;
                            thisAssignment.Status = "New";
                            thisAssignment.Ci = assCI.Text;
                            thisAssignment.Requestor = assRequestor.Text;
                            thisAssignment.Subjecttype = assSubjectType.Text;
                            thisAssignment.Accountnames = name;
                            thisAssignment.Type = i;

                            assm.InsertAssign(thisAssignment);
                            DialogResult msg1 = MessageBox.Show("Assignment has been added to the Database", "New Assignment", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            String parameter = "";
            try
            {
                dt = new DataTable();
                dt.Columns.Add(new DataColumn("Assignment No.", typeof(int)));
                dt.Columns.Add(new DataColumn("RCBC C.I.", typeof(string)));
                dt.Columns.Add(new DataColumn("Account Name(s)", typeof(string)));
                dt.Columns.Add(new DataColumn("Requesting Unit", typeof(string)));
                dt.Columns.Add(new DataColumn("Subject Name", typeof(string)));
                dt.Columns.Add(new DataColumn("Subject Type", typeof(string)));
                dt.Columns.Add(new DataColumn("Batch No.", typeof(int)));
                dt.Columns.Add(new DataColumn("Start Date", typeof(string)));
                dt.Columns.Add(new DataColumn("End Date", typeof(string)));
                dt.Columns.Add(new DataColumn("Address", typeof(string)));
                dt.Columns.Add(new DataColumn("City/Municipality", typeof(string)));
                dt.Columns.Add(new DataColumn("Employee", typeof(string)));
                dt.Columns.Add(new DataColumn("Employee Id", typeof(int)));
                dt.Columns.Add(new DataColumn("Description", typeof(string)));
                dt.Columns.Add(new DataColumn("Status", typeof(string)));
                dt.Columns.Add(new DataColumn("Type", typeof(int)));

                if (assEFilter.Visible == true)
                {
                    if (assEFilter.Text == "" || assEditFilter.Text == "Select Filter")
                    {
                        DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        if (validator.IsSafeString(assEFilter.Text))
                        {
                            if (assEditFilter.SelectedItem.Equals("Employee Name"))
                            {
                                parameter = "e_name";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }

                            if (assEditFilter.SelectedItem.Equals("Employee ID"))
                            {
                                parameter = "e_id";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }

                            if (assEditFilter.SelectedItem.Equals("Assign ID"))
                            {
                                parameter = "assign_id";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }

                            if (assEditFilter.SelectedItem.Equals("Requesting Unit"))
                            {
                                parameter = "requestingunit";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }

                            if (assEditFilter.SelectedItem.Equals("Description"))
                            {
                                parameter = "assignment";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }

                            if (assEditFilter.SelectedItem.Equals("Batch No"))
                            {
                                parameter = "batchno";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }

                            if (assEditFilter.SelectedItem.Equals("Status"))
                            {
                                parameter = "status";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }

                            if (assEditFilter.SelectedItem.Equals("Address"))
                            {
                                parameter = "address";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }
                            if (assEditFilter.SelectedItem.Equals("City"))
                            {
                                parameter = "city";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }
                            if (assEditFilter.SelectedItem.Equals("Account Name"))
                            {
                                parameter = "accountname";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }
                            if (assEditFilter.SelectedItem.Equals("Subject Name"))
                            {
                                parameter = "subjectname";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }
                            if (assEditFilter.SelectedItem.Equals("Subject Type"))
                            {
                                parameter = "subjecttype";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }
                            if (assEditFilter.SelectedItem.Equals("C.I."))
                            {
                                parameter = "ci";
                                assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                                sana = assm.FilterAssign(assEFilter.Text, parameter);
                            }
                        }
                        else
                        {
                           MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                    }
                }
                else if (searchDateE.Visible == true)
                {
                    if (assEditFilter.SelectedItem.Equals("Start Date"))
                    {
                        parameter = "startdate";
                        assEditDisplay.DataSource = assm.FilterAssign(searchDateE.Text, parameter);
                        sana = assm.FilterAssign(searchDateE.Text, parameter);
                    }
                    else if (assEditFilter.SelectedItem.Equals("End Date"))
                    {
                        parameter = "enddate";
                        assEditDisplay.DataSource = assm.FilterAssign(searchDateE.Text, parameter);
                        sana = assm.FilterAssign(searchDateE.Text, parameter);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String parameter = "";
            try
            {
                dt = new DataTable();
                dt.Columns.Add(new DataColumn("Assignment No.", typeof(int)));
                dt.Columns.Add(new DataColumn("Requesting Unit", typeof(string)));
                dt.Columns.Add(new DataColumn("Batch No.", typeof(int)));
                dt.Columns.Add(new DataColumn("Start Date", typeof(string)));
                dt.Columns.Add(new DataColumn("End Date", typeof(string)));
                dt.Columns.Add(new DataColumn("Address", typeof(string)));
                dt.Columns.Add(new DataColumn("City/Municipality", typeof(string)));
                dt.Columns.Add(new DataColumn("Employee", typeof(string)));
                dt.Columns.Add(new DataColumn("Employee Id", typeof(int)));
                dt.Columns.Add(new DataColumn("Description", typeof(string)));
                dt.Columns.Add(new DataColumn("Status", typeof(string)));

                if (assVFilter.Visible == true)
                {
                    if (assVFilter.Text == "" || assViewFilter.Text == "Select Filter")
                    {
                        DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        if (validator.IsSafeString(assVFilter.Text))
                        {
                            if (assViewFilter.SelectedItem.Equals("Employee Name"))
                            {
                                parameter = "e_name";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("Employee ID"))
                            {
                                parameter = "e_id";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("Assign ID"))
                            {
                                parameter = "assign_id";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);

                            }

                            if (assViewFilter.SelectedItem.Equals("Requesting Unit"))
                            {
                                parameter = "requestingunit";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("Description"))
                            {
                                parameter = "assignment";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("Batch No"))
                            {
                                parameter = "batchno";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("Status"))
                            {
                                parameter = "status";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("Start Date"))
                            {
                                parameter = "startdate";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("End Date"))
                            {
                                parameter = "endate";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }

                            if (assViewFilter.SelectedItem.Equals("Address"))
                            {
                                parameter = "address";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }
                            if (assViewFilter.SelectedItem.Equals("City"))
                            {

                                parameter = "city";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }
                            if (assViewFilter.SelectedItem.Equals("Account Name"))
                            {
                                parameter = "accountname";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }
                            if (assViewFilter.SelectedItem.Equals("Subject Name"))
                            {
                                parameter = "subjectname";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }
                            if (assViewFilter.SelectedItem.Equals("Subject Type"))
                            {
                                parameter = "subjecttype";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }
                            if (assViewFilter.SelectedItem.Equals("C.I."))
                            {
                                parameter = "ci";
                                assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                            }
                        }
                        else
                        {
                            MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                    }
                }
                else if (searchDateV.Visible == true)
                {
                    if (assViewFilter.SelectedItem.Equals("Start Date"))
                    {
                        parameter = "startdate";
                        assViewDisplay.DataSource = assm.FilterAssign(searchDateV.Text, parameter);
                    }
                    if (assViewFilter.SelectedItem.Equals("End Date"))
                    {
                        parameter = "enddate";
                        assViewDisplay.DataSource = assm.FilterAssign(searchDateV.Text, parameter);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        private void empViewAssignments_Click_1(object sender, EventArgs e)
        {
            empAssList.Visible = true;
            try
            {
                if (empViewList.Text == "")
                {
                    DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    String item = empViewList.Text;
                    String name2, id2;
                    string[] words1 = item.Split('-');

                    name2 = words1[0];
                    id2 = words1[1];

                    empAssList.DataSource = en.ViewAssignments(id2);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void empEditCancel_Click_1(object sender, EventArgs e)
        {
            DialogResult msg1 = MessageBox.Show("Do you want to exit the Edit Employee Application?", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (msg1 == DialogResult.Yes)
            {
                menu.SelectedIndex = 0;
            }
        }

        private void assAddCancel_Click(object sender, EventArgs e)
        {
            DialogResult msg1 = MessageBox.Show("Do you want to Clear the Add Assignment Application?", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            try
            {
                if (msg1 == DialogResult.Yes)
                {
                    assAccounts.Items.Clear();
                    assRequestor.Text = null;
                    assSubjectType.SelectedIndex = -1;
                    assCI.Text = null;
                    assAddRequestingUnit.Text = null;
                    assAddBatchNo.Text = null;
                    startDate.Text = null;
                    endDate.Text = null;
                    assAddAddress.Text = null;
                    assAddCity.Text = null;
                    assAddDesc.Text = null;
                    Type1.Text = "Select Type";
                    AssignBox.DataSource = assm.EmployeeAssign();
                    AssignBox.SelectedIndex = -1;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        private void empAddCancel_Click_1(object sender, EventArgs e)
        {
            DialogResult msg1 = MessageBox.Show("Do you want to exit the Add Employee Application?", "Add", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (msg1 == DialogResult.Yes)
            {
                menu.SelectedIndex = 0;
            }
        }

        private void payPrintDataCV_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you surre you want to print?","Create Excel file",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);

            if (msg == DialogResult.Yes)
            {
                try
                {
                    ex.PrintCV(payDataDisplay);
                    MessageBox.Show("Excel file saved in the Documents folder", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString(),"Fatal",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                }
            }
        }

        private void payPrintDataPList_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you surre you want to print?", "Create Excel file", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (msg == DialogResult.Yes)
            {
                try
                {
                    ex.PrintPayroll(payDataDisplay, listbegin, listend);
                    MessageBox.Show("Excel file saved in the Documents folder", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

        private void payPrintListBIR_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you surre you want to print?", "Create Excel file", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (msg == DialogResult.Yes)
            {
                try
                {
                    if (birdocutype)
                    {
                        ex.PrintBIR(payDataDisplay, biryear);
                    }
                    else
                    {
                        ex.PrintBIRYear(payDataDisplay, biryear);
                    }
                    MessageBox.Show("Excel file saved in the Documents folder", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

        private void payAddopt_Click(object sender, EventArgs e)
        {
            panelPayAdd.Visible = true;
            panelPayModify.Visible = false;
        }

        private void payModifyopt_Click(object sender, EventArgs e)
        {
            List<string> a = new List<string>();
            try
            {
                a = assm.getRates();
                panelPayModify.Visible = true;
                panelPayAdd.Visible = false;
                payRates.Items.Clear();

                for (int i = 0; i < a.Count; i++)
                {
                    payRates.Items.Add(a[i]);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool rateCheck = false;
            int payment = 0;
            try
            {
                if (payRateAdd.Text != "")
                {
                    try
                    {
                        payment = Convert.ToInt32(payRateAdd.Text);
                        rateCheck = assm.typeCheck(payment);

                        if (rateCheck)
                        {
                            assm.addRate(payment);
                            payRateAdd.Text = "";
                            MessageBox.Show("Payment Rate and type successfully added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            payRateAdd.Text = "";
                            MessageBox.Show("This payment rate already belongs to a specific type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                    }
                    catch (Exception err1)
                    {
                        payRateAdd.Text = "";
                        MessageBox.Show(err1.Message+"\nError Stack: "+err1.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    MessageBox.Show("Please enter a valid payment rate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            List<string> a = new List<string>();
            string s = payRates.SelectedItem.ToString();
            string[] splitter = s.Split('-');
            int type;
            int payment;
            bool ratecheck = false;

            try
            {
                if (payRateModify.Text != "")
                {
                    try
                    {
                        type = Convert.ToInt32(splitter[0]);
                        payment = Convert.ToInt32(payRateModify.Text);

                        ratecheck = assm.typeCheck(payment);

                        if (ratecheck)
                        {
                            assm.modRate(payment, type);
                            payRateModify.Text = "";
                            MessageBox.Show("Payment Rate and type successfully updated", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            payRates.Items.Clear();
                            a = assm.getRates();
                            for (int i = 0; i < a.Count; i++)
                            {
                                payRates.Items.Add(a[i]);
                            }
                        }
                        else
                        {
                            payRateModify.Text = "";
                            MessageBox.Show("This payment rate already belongs to a specific type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                    }
                    catch (Exception err3)
                    {
                        payRateModify.Text = "";
                        MessageBox.Show("Please enter a valid value, only numerical values are allowed\n"+err3.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    MessageBox.Show("Please enter a valid payment rate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void assAccAdd_Click(object sender, EventArgs e)
        {
            form = this;

            this.Enabled = false;

            addAccountName addname = new addAccountName();
            addname.Show();
        }

        public Boolean addAccountName(string name)
        {
            Boolean allowed = true;

            try
            {
                foreach (string accname in assAccounts.Items)
                {
                    if (accname == name)
                    {
                        allowed = false;
                    }
                }

                if (allowed)
                {
                    assAccounts.Items.Add(name);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + "/nError Stack: " + err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                allowed = false;
            }

            return allowed;
        }

        public Boolean checkmodifiedname(string name)
        {
            Boolean allowed = true;

            foreach (string accname in assAccounts.Items)
            {
                if (accname == name)
                {
                    allowed = false;
                }
            }

            return allowed;
        }

        public void editAccountName(string name, string old)
        {
            for (int i = 0; assAccounts.Items.Count > i; i++)
            {
                if (assAccounts.Items[i].Equals(old))
                {
                    assAccounts.Items[i] = name;
                }
            }
        }

        private void assAccModify_Click(object sender, EventArgs e)
        {
            if (assAccounts.SelectedIndex != -1)
            {
                if (assAccounts.Items.Count > 0)
                {
                    string oldname = assAccounts.SelectedItem.ToString();
                    form = this;

                    this.Enabled = false;

                    modifyAccountName editname = new modifyAccountName(oldname);
                    editname.Show();
                }
                else
                {
                    MessageBox.Show("There is no item in the list", "List Empty", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("No name selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void assAccDelete_Click(object sender, EventArgs e)
        {
            if (assAccounts.SelectedIndex != -1)
            {
                assAccounts.Items.Remove(assAccounts.SelectedItem);
            }
            else
            {
                MessageBox.Show("No name selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }

        }

        private void assViewFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (assViewFilter.SelectedItem.Equals("Start Date") || assViewFilter.SelectedItem.Equals("End Date"))
            {
                assVFilter.Visible = false;
                searchDateV.Visible = true;
                button3.Location = new Point(213, 2);
            }
            else
            {
                assVFilter.Visible = true;
                searchDateV.Visible = false;
                button3.Location = new Point(113, 2);
            }
        }

        private void assEditFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (assEditFilter.SelectedItem.Equals("Start Date") || assEditFilter.SelectedItem.Equals("End Date"))
            {
                assEFilter.Visible = false;
                searchDateE.Visible = true;
                button2.Location = new Point(213, 2);
            }
            else
            {
                assEFilter.Visible = true;
                searchDateE.Visible = false;
                button2.Location = new Point(113, 2);
            }
        }

        private void importexcel_Click(object sender, EventArgs e)
        {
            form = this;
            documentselect select = new documentselect();
            select.Show();
            this.Enabled = false;
        }

        private void menu_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.MaximizeBox = false;
            panelAssAdd.Visible = false;
            panelAssEdit.Visible = false;
            panelAssView.Visible = false;
            panelEditAccount.Visible = false;
            panelEditPassword.Visible = false;
            panelEditSecurity.Visible = false;
            panelGenerate.Visible = false;
            panelLoginHistory.Visible = false;
            panelPayAdd.Visible = false;
            panelPayModify.Visible = false;
            panelPayUpdate.Visible = false;
            panelPayView.Visible = false;
            panelTerminate.Visible = false;
            panelViewAccts.Visible = false;
            ViewBox.Visible = false;
            AddBox.Visible = false;
            EditBox.Visible = false;
            payGenCV.Enabled = false;
            viewGeneratedCV.Visible = false;
            paySearchBar.Enabled = false;
            payPrintSearch.Enabled = false;
            payPrintDataCV.Visible = false;
            payPrintDataPList.Visible = false;
            payPrintListBIR.Visible = false;
            if (menu.SelectedIndex == 5)
            {
                MessageBox.Show("Function not yet supported", "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                menu.SelectedIndex = 0;
            }
        }
    }
}

